package com.example.patients;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.patients.adapter.Adapter;
import com.example.patients.database.DbPatients;
import com.example.patients.entity.PatientsBean;
import java.util.ArrayList;
import java.util.List;

public class SearchPatientsActivity extends AppCompatActivity implements View.OnClickListener {
    ImageButton BtnBack;//退出按钮
    ListView listView;//列表
    List<PatientsBean> mData;//数据源
    Adapter adapter;//适配器
    DbPatients dbPatients;//数据库
    TextView TotalTest;//总人数显示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_patients);//绑定事件
        BtnBack = findViewById(R.id.btn_back);
        BtnBack.setOnClickListener(this);
        listView = findViewById(R.id.listView);
        dbPatients = new DbPatients(this);
        mData = new ArrayList<>();

        //设置适配器：加载每一行数据到列表当中
        adapter = new Adapter(this, mData);
        listView.setAdapter(adapter);
        loadData();//显示病人信息列表函数
        TotalTest = findViewById(R.id.tv_total_count);
        TotalTest.setText("住院总人数:  "+ dbPatients.getTotalPatientCount());
    }
    /**
     * 显示病人信息列表
     */
    private void loadData() {
        List<PatientsBean> list = DbPatients.search();
        mData.clear();
        mData.addAll(list);
        adapter.notifyDataSetChanged();//实时更新
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.btn_back){
            Intent intent = new Intent(SearchPatientsActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
